package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dto) throws CloneNotSupportedException {
        log.info("新增菜品传递的参数: {}", dto);
        return dishService.save(dto);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO queryDto){
        log.info("分页查询菜品: {}", queryDto);

        return dishService.page(queryDto);
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品: {}", ids);

        return dishService.delete(ids);
    }

    @GetMapping("/{dishId}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long dishId){
        log.info("根据id查询菜品: {}", dishId);
        return dishService.getById(dishId);
    }

    @PutMapping
    @ApiOperation("根据id修改菜品")
    public Result<String> update(@RequestBody DishDTO dto){
        log.info("根据id修改菜品: {}", dto);

        return dishService.update(dto);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
